$(function () {
    var markid = getUrlString('markid');let show,isUse=0;
    cashCommodityList(0);
    function cashCommodityList () {
        layLoading();
        var data = {
            'markid':markid,
            'page':0
        };
        getData('post','/niu/apiv4/renewal_mytoylist.php',data,function (data) {
            console.log(data);
            layer.closeAll('loading');
            if(data.resultcode === "0" ){
                if (data.datacount === "0"){
                    $('#loaded').html('暂无可续期商品')
                }else {
                    $('#loaded').html('没有更多数据了')
                }
                var append = '';

                $(data.data).each(function () {
                    append += '<li>' +
                        '            <input class="kuid" type="hidden" value="'+this.id+'">'+
                        '            <input class="toysid" type="hidden" value="'+this.toysid+'">'+
                        '            <div class="pic">' +
                        '                <img src="'+this.pics+'-toysbox11'+'" alt="">' +
                        '                <img class="noCheck" src="../../res/img/my/myEggPool/noCheck.png" alt="">' +
                        '                <img class="check" src="../../res/img/my/myEggPool/check.png" alt="">' +
                        '                        <div class="dayNum">'+this.outtime+'</div>' +
                        '            </div>' +
                        '            <div class="name">'+this.name+'</div>' +
                        '        </li>'
                });
                $('#cashCommodityList').append(append);
                $('#loaded').show();
                //点击选择商品
                $('#cashCommodityList li').click(function () {
                    if($(this).hasClass('selected')){
                        $(this).removeClass('selected');
                        $(this).find('.pic').children('.check').hide();
                        $(this).children('.kuid').removeClass('forId');
                        $(this).children('.toysid').removeClass('forId2');
                    }else {
                        $(this).addClass('selected');
                        $(this).find('.pic').children('.check').show();
                        $(this).children('.kuid').addClass('forId');
                        $(this).children('.toysid').addClass('forId2');
                    }
                });

                //全选
                $('.checkAllSelected').click(function () {
                    $(this).hide();
                    $('.cashCommodityList li').addClass('selected');
                    $('.kuid').addClass('forId');
                    $('.toysid').addClass('forId2');
                    $('.check,.noCheckAll').show();
                });

                //取消全选
                $('.noCheckAll').click(function () {
                    $('.check,.noCheckAll').hide();
                    $('.cashCommodityList li').removeClass('selected');
                    $('.kuid').removeClass('forId');
                    $('.toysid').removeClass('forId2');
                    $('.checkAllSelected').show();
                });
                // mRefresh.endUpLoading(true);
            }else {
                laymsg(data.msg)
            }
        })
    }
    let cupNum,day,pickUpStr,pickUpStr2;
    //判断是否有续期卷
    function cupList() {
        let data = {
            'markid':markid,
            'type':1,
        };
        getData('post','/niu/apiv4/fahuo_dosend_my_coupon.php',data,function (data) {
            console.log(data);
            //包邮劵
            cupNum = Number(data.info[0].num);
            var money2 = cupNum * 14;
            if (cupNum > 0){
                $('#cupNum').show();
                $('#renewalNum').html('使用'+cupNum+'张续期券抵扣'+money2+'金币')
            }else {
                $('#cupNum').hide();
            }
        })
    }

    cupList();
    //七天续期
    $('#seven').click(function () {
        day = 7;
        let pickUpArr = [];
        let pickUpArr2 = [];
        $('.forId').each(function () {
            pickUpArr.push($(this).val())
        });
        $('.forId2').each(function () {
            pickUpArr2.push($(this).val())
        });
        pickUpStr = pickUpArr.join(',');
        pickUpStr2 = pickUpArr2.join(',');
        if (isUse === 0){
            renewal(day,cupNum,pickUpStr,pickUpStr2);
        } else {
            renewal(day,0,pickUpStr,pickUpStr2);
        }
    });


    //14天续期
    $('#twoSeven').click(function () {
        day = 14;
        let pickUpArr = [];
        let pickUpArr2 = [];
        $('.forId').each(function () {
            pickUpArr.push($(this).val())
        });
        $('.forId2').each(function () {
            pickUpArr2.push($(this).val())
        });
        pickUpStr = pickUpArr.join(',');
        pickUpStr2 = pickUpArr2.join(',');
        if (isUse === 0){
            renewal(day,cupNum,pickUpStr,pickUpStr2);
        } else {
            renewal(day,0,pickUpStr,pickUpStr2);
        }

    });

    //28天续期
    $('#fourSeven').click(function () {
        day = 28;
        let pickUpArr = [];
        let pickUpArr2 = [];
        $('.forId').each(function () {
            pickUpArr.push($(this).val())
        });
        $('.forId2').each(function () {
            pickUpArr2.push($(this).val())
        });
        pickUpStr = pickUpArr.join(',');
        pickUpStr2 = pickUpArr2.join(',');
        if (isUse === 0){
            renewal(day,cupNum,pickUpStr,pickUpStr2);
        } else {
            renewal(day,0,pickUpStr,pickUpStr2);
        }
    });

    let isAjax = true;
    function renewalBtn (day,checkNum,goodids,money2,toysids) {
        if (isAjax){
            isAjax = false;
            if (show === 0){
                laymsg('请同意协议');
                return
            }
            let data = {
                'markid':markid,
                'goodids':goodids,
                'money2':money2,
                'days':day,
                'toysids':toysids,
                'renewalNum':checkNum
            };
            getData('post','/niu/apiv4/renewal_gold.php',data,function (data) {
                console.log(data);
                if (data.resultcode === 200){
                    laymsg(data.msg);
                    setTimeout(function () {
                        window.location.reload();
                    },500)
                }else {
                    isAjax = true;
                    laymsg(data.msg);
                }

            })
        }

    }
    let useCup,price,goodid;
    function renewal (day,cupNumber,goodids){
        goodid = goodids;
        let checkNum = $('.forId').length;
        let moneyNum = checkNum * 2 *day;
        $('#renewalTime').html('共计'+checkNum+'件商品延长'+day+'天');
        $('#money2').html(moneyNum);
        if (checkNum === 0){
            laymsg('请选择续期商品');
            return
        }

        //七天续期
        if (day ===7){

            if (checkNum < cupNumber || checkNum === cupNumber){
                // let money2 = checkNum * 7 * 2;
                // $('#renewalNum').html('使用'+checkNum+'张续期券抵扣'+money2+'金币');
                price = 0;
                useCup = checkNum;
                $('#qrBtn').html('确认支付');
                $('.alertIndex').show();
            }else {
                // let money2 = cupNumber * 7 * 2;
                // $('#renewalNum').html('使用'+checkNum+'张续期券抵扣'+money2+'金币');
                price = (checkNum - cupNumber)* 14;
                useCup = cupNumber;
                $('#qrBtn').html('确认支付'+price+'金币');
                $('.alertIndex').show();
            }

        }else if (day === 14) {
            let checkNumTwo = checkNum * 2;
            if (checkNumTwo < cupNumber || checkNumTwo === cupNumber){
                // let money2 = checkNumTwo * 7 * 2;
                // $('#renewalNum').html('使用'+checkNumTwo+'张续期券抵扣'+money2+'金币');
                //使用续期劵支付金币
                useCup = checkNumTwo;
                price = 0;
                $('#qrBtn').html('确认支付');
                $('.alertIndex').show();
            }else {
                // let money2 = cupNumber * 7 * 2;
                // $('#renewalNum').html('使用'+checkNum+'张续期券抵扣'+money2+'金币');
                price = (checkNumTwo * 14) - (cupNumber * 14);
                useCup = cupNumber;
                $('#qrBtn').html('确认支付'+price+'金币');
                $('.alertIndex').show();
            }
        }else if(day === 28){
            let checkNumTwo = checkNum * 4;
            if (checkNumTwo < cupNumber || checkNumTwo === cupNumber){
                // let money2 = checkNumTwo * 7 * 2;
                // $('#renewalNum').html('使用'+checkNumTwo+'张续期券抵扣'+money2+'金币');
                useCup = checkNumTwo;
                price = 0;
                $('#qrBtn').html('确认支付');
                $('.alertIndex').show();
            }else {
                price = (checkNumTwo * 14) - (cupNumber * 14);
                useCup = cupNumber;
                $('#qrBtn').html('确认支付'+price+'金币');
                $('.alertIndex').show();
            }
        }

        let money = useCup * 7 * 2;
        $('#renewalNum').html('使用'+useCup+'张续期券抵扣'+money+'金币');

    }

    $('#qrBtn').click(function () {
        if (isUse === 0){
            // alert(pickUpStr2)
            renewalBtn(day,useCup,goodid,price,pickUpStr2);
        } else {
            renewalBtn(day,'0',goodid,price,pickUpStr2);
        }

    });

    //续期卷的使用
    $('.guaSmell .isNoty').click(function () {
        $(this).hide();
        $('.tyWXS').show();
        isUse = 0;
        renewal(day,cupNum,pickUpStr)
    });

    $('.guaSmell .isTy').click(function () {
        $(this).hide();
        $('.notyWXS').css('display','inline-block');
        isUse = 1;
        renewal(day,0,pickUpStr)
    });



    //同意协议
    $('.notyxdxy').click(function () {
        $(this).hide();
        $('.tyxdxy').show();
        show = 1;
    });
    //不同意协议
    $('.tyxdxy').click(function () {
        show = 0;
        $(this).hide();
        $('.notyxdxy').css('display','inline-block')
    });


    $('#closeAlert').click(function () {
        $('.alertIndex').hide()
    });

    nativeCommand('dancool://titlebar=righttext|说明|nativeCommand("dancool://web='+urlView+'agreement/renelwal.html?type=1")')

});